package cn.bupt.adt.controller;

import cn.bupt.adt.constant.Constants;
import cn.bupt.adt.entity.autogenerate.DoctorAppUser;
import cn.bupt.adt.entity.autogenerate.Order;
import cn.bupt.adt.entity.autogenerate.OrderView;
import cn.bupt.adt.service.IDoctorService;
import cn.bupt.adt.service.IOrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 管理后台订单接口
 */
@Controller
@RequestMapping("/orderManagement")
public class OrderController {

    @Autowired
    IOrderService orderService;

    @Autowired
    IDoctorService doctorService;

    //所有订单
    @RequestMapping("/index/{pageNum}")
    public ModelAndView getAllOrders(@PathVariable("pageNum") int pageNum) {
        ModelAndView mv = new ModelAndView(Constants.ALL_ORDER);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE_LARGE, "publicTime desc");
        List<OrderView> orders = orderService.getAllOrders();
        mv.addObject("orders", orders);
        mv.addObject("allPages", page.getPages());
        mv.addObject("currentPage", page.getPageNum());
        return mv;
    }

    //进行中的订单 publishStatus为0,1的统称为进行中的订单
    @RequestMapping("/onGoUntaked/{pageNum}")
    public ModelAndView onGo(@PathVariable("pageNum") int pageNum) {
        ModelAndView mv = new ModelAndView(Constants.ONGOUntaked_ORDER);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE_LARGE, "publicTime desc");
        List<OrderView> orders = orderService.getUntakedOrders();
        mv.addObject("orders", orders);
        mv.addObject("allPages", page.getPages());
        mv.addObject("currentPage", page.getPageNum());
        return mv;
    }
    //未接单订单  等同 onGo订单
//		@RequestMapping("/unTaked/{pageNum}")
//		public ModelAndView getUntakedOrders(@PathVariable("pageNum") int pageNum) {
//			ModelAndView mv = new ModelAndView(Constants.UNTAKED_ORDER);
//			Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE, "publicTime desc");
//			List<OrderView> orders = orderService.getUntakedOrders();
//			mv.addObject("orders", orders);
//			mv.addObject("allPages", page.getPages());
//			mv.addObject("currentPage", page.getPageNum());
//			return mv;
//		}

    //超时订单
    @RequestMapping("/overTimeOrder/{pageNum}")
    public ModelAndView getOverTimeOrders(@PathVariable("pageNum") int pageNum) {
        ModelAndView mv = new ModelAndView(Constants.OVERTIME_ORDER);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE_LARGE, "publicTime desc");
        List<OrderView> orders = orderService.getOverTimeOrders();
//		System.out.println(orders);
        mv.addObject("orders", orders);
        mv.addObject("allPages", page.getPages());
        mv.addObject("currentPage", page.getPageNum());
        return mv;
    }

    //已接单还未开始进行手术的订单
    @RequestMapping("/takedAndUnoperated/{pageNum}")
    public ModelAndView getTakedAndUnoperatedOrders(@PathVariable("pageNum") int pageNum) {
        ModelAndView mv = new ModelAndView(Constants.TAKEDANDUNOPERATED_ORDER);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE_LARGE, "publicTime desc");
        List<OrderView> ordersSource = orderService.getTakedAndUnoperatedOrders();

        mv.addObject("orders", ordersSource);
        mv.addObject("allPages", page.getPages());
        mv.addObject("currentPage", page.getPageNum());
        return mv;
    }

    //已接单还未开始进行手术，订单超时的订单
    @RequestMapping("/takedAndUnopOverTime/{pageNum}")
    public ModelAndView getTakedAndUnopOverTimeOrders(@PathVariable("pageNum") int pageNum) {
//			ModelAndView mv = new ModelAndView(Constants.TAKEDANDUNOPERATED_ORDER);
        ModelAndView mv = new ModelAndView(Constants.TAKEDANDUNOPERATED_ORDER);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE_LARGE, "publicTime desc");
//			List<OrderView> orders = orderService.getTakedAndUnopOverTimeOrders();
        List<OrderView> ordersSource = orderService.getTakedAndUnoperatedOrders();
        Date nowDate = new Date();//当前时间\r
        long nowTime = nowDate.getTime();
        for (int i = 0; i < ordersSource.size(); i++) {
            if (nowTime - ordersSource.get(i).getStartTime().getTime() < 0) {
                ordersSource.get(i).setStatus(8);
            }
        }
        mv.addObject("orders", ordersSource);
        mv.addObject("allPages", page.getPages());
        mv.addObject("currentPage", page.getPageNum());
        return mv;
    }

    //已接单开始进行手术的订单
    @RequestMapping("/takedAndOperating/{pageNum}")
    public ModelAndView getTakedAndOperatingOrders(@PathVariable("pageNum") int pageNum) {
        ModelAndView mv = new ModelAndView(Constants.TAKEDANDOPERATING_ORDER);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE_LARGE, "publicTime desc");
        List<OrderView> orders = orderService.getTakedAndOperatingOrders();
        mv.addObject("orders", orders);
        mv.addObject("allPages", page.getPages());
        mv.addObject("currentPage", page.getPageNum());
        return mv;
    }

    //已接单医生已经确认完成的订单
    @RequestMapping("/takedAndFinished/{pageNum}")
    public ModelAndView getTakedAndFinishedOrders(@PathVariable("pageNum") int pageNum) {
        ModelAndView mv = new ModelAndView(Constants.TAKEDANDFINISHEDED_ORDER);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE_LARGE, "publicTime desc");
        List<OrderView> orders = orderService.getTakedAndFinishedOrders();
        mv.addObject("orders", orders);
        mv.addObject("allPages", page.getPages());
        mv.addObject("currentPage", page.getPageNum());
        return mv;
    }

    //求助订单
    @RequestMapping("/takedAndAskForHelp/{pageNum}")
    public ModelAndView getTakedAndAskForHelpOrders(@PathVariable("pageNum") int pageNum) {
        ModelAndView mv = new ModelAndView(Constants.TAKEDANDASKFORHELPED_ORDER);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE_LARGE, "publicTime desc");
        List<OrderView> orders = orderService.getTakedAndAskForHelpOrders();
        mv.addObject("orders", orders);
        mv.addObject("allPages", page.getPages());
        mv.addObject("currentPage", page.getPageNum());
        return mv;
    }

    //完成订单（医生和机构双方已经确认）
    @RequestMapping("/finished/{pageNum}")
    public ModelAndView getFinishedOrders(@PathVariable("pageNum") int pageNum) {
        ModelAndView mv = new ModelAndView(Constants.FINISHED_ORDER);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE_LARGE, "publicTime desc");
        List<OrderView> orders = orderService.getFinishedOrders();
        mv.addObject("orders", orders);
        mv.addObject("allPages", page.getPages());
        mv.addObject("currentPage", page.getPageNum());
        return mv;
    }

    //取消订单
    @RequestMapping("/canceled/{pageNum}")
    public ModelAndView getCanceledOrders(@PathVariable("pageNum") int pageNum) {
        ModelAndView mv = new ModelAndView(Constants.CANCELED_ORDER);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE_LARGE, "publicTime desc");
        List<OrderView> orders = orderService.getCanceledOrders();
        mv.addObject("orders", orders);
		/*for(int i=0;i<orders.size();i++){
			System.out.println(orders.get(i).getId()+"医生姓名="+orders.get(i).getDoctorName());
			System.out.println(orders.get(i).getId()+"医生电话="+orders.get(i).getDoctorPhone());
		}*/

        mv.addObject("allPages", page.getPages());
        mv.addObject("currentPage", page.getPageNum());
        return mv;
    }

    //查看未接单订单详情
    @RequestMapping("/unTakedDetail/{id}")
    public ModelAndView unTakedDetail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.unTaked_DETAIL);
        OrderView order = orderService.getOrderViewById(id);
        mv.addObject("order", order);
        return mv;
    }

    //查看接单未手术订单详情
    @RequestMapping("/takedAndUDetail/{id}")
    public ModelAndView tackedAndUDetail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.takedAndU_DETAIL);
        OrderView order = orderService.getOrderViewById(id);
        mv.addObject("order", order);
        return mv;
    }

    //	//查看接单未手术订单超时详情
//			@RequestMapping("/tackedAndUOverTimeDetail/{id}")
//			public ModelAndView tackedAndUOverTimeDetail(@PathVariable("id") int id) {
//				ModelAndView mv = new ModelAndView(Constants.takedAndUOverTime_DETAIL);
//				OrderView order = orderService.getOrderViewById(id);
//				mv.addObject("order", order);
//				return mv;
//			}
    //查看手术中订单详情
    @RequestMapping("/takedAndODetail/{id}")
    public ModelAndView tackedAndODetail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.takedAndO_DETAIL);
        OrderView order = orderService.getOrderViewById(id);
        mv.addObject("order", order);
        return mv;
    }

    //查看求助订单详情
    @RequestMapping("/takedAndADetail/{id}")
    public ModelAndView tackedAndADetail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.takedAndA_DETAIL);
        OrderView order = orderService.getOrderViewById(id);
        mv.addObject("order", order);
        return mv;
    }

    //查看取消订单详情
    @RequestMapping("/canceledDetail/{id}")
    public ModelAndView canceledDetail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.canceled_DETAIL);
        OrderView order = orderService.getOrderViewById(id);
        mv.addObject("order", order);
        System.out.println("cancelBy==" + order.getCancelBy());
        System.out.println("CancelReason==" + order.getCancelReason());//510个字符限制
        return mv;
    }

    //查看已完成机构未确认订单详情
    @RequestMapping("/finishDetail/{id}")
    public ModelAndView finishDetail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.FINISH_DETAIL);
        OrderView order = orderService.getOrderViewById(id);
        mv.addObject("order", order);
        return mv;
    }

    //查看已完成机构确认详情
    @RequestMapping("/finishedDetail/{id}")
    public ModelAndView finishedDetail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.FINISHED_DETAIL);
        OrderView order = orderService.getOrderViewById(id);
        mv.addObject("order", order);
        return mv;
    }

    //查看订单详情
    @RequestMapping("/indexDetail/{id}")
    public ModelAndView indexDetail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.INDEX_DETAIL);
        OrderView order = orderService.getOrderViewById(id);
        mv.addObject("order", order);
        return mv;
    }

    //查看超时订单详情
    @RequestMapping("/overTimeDetail/{id}")
    public ModelAndView orderDetail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.overTime_DETAIL);
        OrderView order = orderService.getOrderViewById(id);
        Order order2 = orderService.getOrderById(id);
        List<DoctorAppUser> doctorList = doctorService.getDoctorsByOrder(order2);
        mv.addObject("order", order);
        mv.addObject("doctorList", doctorList);
        return mv;
    }


    //有变动订单（医生和机构双方已经确认）
    @RequestMapping("/changed/{pageNum}")
    public ModelAndView getChangedOrders(@PathVariable("pageNum") int pageNum) {
        ModelAndView mv = new ModelAndView(Constants.CHANGED_ORDER);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE_LARGE, "publicTime desc");
        List<OrderView> orders = orderService.getChangedOrders();
        mv.addObject("orders", orders);
        mv.addObject("allPages", page.getPages());
        mv.addObject("currentPage", page.getPageNum());
        return mv;
    }

    //查看有变动订单详情
    @RequestMapping("/changedDetail/{id}")
    public ModelAndView changedOrderDetail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.CHANGEED_DETAIL);
        OrderView order = orderService.getOrderViewById(id);
        Order order2 = orderService.getOrderById(id);
        List<DoctorAppUser> doctorList = doctorService.getDoctorsByOrder(order2);
        mv.addObject("order", order);
        mv.addObject("doctorList", doctorList);
        return mv;
    }

    /**
     * @param order 指派医生通知
     * @return
     * @throws Exception
     */
    @RequestMapping("/indexDetailPost")
    public ModelAndView addProductPost(OrderView order,
                                       HttpServletRequest request) throws Exception {
        ModelAndView mv = new ModelAndView(Constants.overTime_DETAIL);
        if (order.getDoctorId() != null) {
            int rst = doctorService.appointDoctor(order.getDoctorId());
            // 指派成功
            if (rst == 1) {
                request.setAttribute("resultMsg", "指派成功");
                mv = new ModelAndView("forward:/orderManagement/overTimeOrder/1");
            } else {
                request.setAttribute("resultMsg", "指派失败");
                mv = new ModelAndView("forward:/orderManagement/overTimeOrder/1");//overTimeDetail/"+order.getId());
            }
        } else {
            request.setAttribute("resultMsg", "指派失败,未选择医生");
            mv = new ModelAndView("forward:/orderManagement/overTimeOrder/1");//overTimeDetail/"+order.getId());
        }

        return mv;

    }

}
